package com.tj.jinzao.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseActivity;
import com.tj.jinzao.fragment.BindPhoneFragment;
import com.tj.jinzao.fragment.InviteCodeFragment;
import com.tj.jinzao.fragment.PasswordLoginFragment;
import com.tj.jinzao.fragment.SetPasswordFragment;
import com.tj.jinzao.fragment.VerifyInputFragment;
import com.tj.jinzao.fragment.VerifyLoginFragment;

/**
 * 登录注册界面
 * Created by wjy on 2019/1/5.
 */
public class LoginActivity extends BaseActivity {

    public final static int LOGIN_TYPE_VERITY_LOGIN = 0;//验证码登录
    public final static int LOGIN_TYPE_PASSWORD_LOGIN = 1;//密码登录
    public final static int LOGIN_TYPE_SOCIAL_PHONE = 2;//第三方登录绑定手机号
    public final static int LOGIN_TYPE_FORGET_CODE = 3;//忘记密码
    public final static int LOGIN_TYPE_INPUT_VERIFY_CODE = 4;//输入验证码,登录
    public final static int LOGIN_TYPE_INPUT_RESET_CODE = 5;//重置密码
    public final static int LOGIN_TYPE_INPUT_VERIFY_CODE_RESET = 6;//输入验证码,重置密码
    public final static int LOGIN_TYPE_SET_PASSWORD = 7;//设置密码
    public final static int LOGIN_TYPE_INVITE = 8;//邀请码

    private Fragment mBindPhoneFragment,mVerifyLoginFragment, mPasswordLoginFragment, mVerifyInputFragment, mSetPasswordFragment, mInviteFragment;
    private SetPasswordFragment mResetPasswordFragment;

    public final static String ARG_TOKEN = "token";
    public final static String ARG_CODE = "code";
    public final static String ARG_PHONE = "phone";
    public final static String ARG_INPUT_TYPE = "input_type";

    private int mSocialType;//第三方登录类型
    private int mInputCodeType;//输入验证码类型

    private Fragment mCurrentFragment;

    private String mPhone, mToken, mSmsCode;
    //typeLogin为6：更换绑定手机号，暂时没有用到

    @Override
    protected void setupView() {
        super.setupView();
        if(getIntent()!=null&&getIntent().getIntExtra("typeLogin",0)==1){
            switchFragment(LOGIN_TYPE_SOCIAL_PHONE);
        }else if(getIntent()!=null&&getIntent().getIntExtra("typeLogin",0)==2){
            switchFragment(LOGIN_TYPE_INVITE);
        }else if(getIntent()!=null&&getIntent().getIntExtra("typeLogin",0)==6){
            switchFragment(LOGIN_TYPE_SOCIAL_PHONE);
        }else {
            switchFragment(LOGIN_TYPE_VERITY_LOGIN);
        }

    }

    public void setPhone(String phone) {
        this.mPhone = phone;
    }

    public void setSmsCode(String smsCode) {
        this.mSmsCode = smsCode;
    }

    public void setToken(String token) {
        this.mToken = token;
    }

    @Override
    public int getLayoutResource() {
        return R.layout.base_activity_fragment_no_title_layout;
    }

    public void switchFragment(int type) {
        //        if (mIndex == index) return;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (mCurrentFragment != null) transaction.hide(mCurrentFragment);
        switch (type) {
            case LOGIN_TYPE_VERITY_LOGIN:
                if (mVerifyLoginFragment == null) {
                    mVerifyLoginFragment = new VerifyLoginFragment();
                }
                mCurrentFragment = mVerifyLoginFragment;
                break;
            case LOGIN_TYPE_SOCIAL_PHONE:
                if (mBindPhoneFragment == null) {
                    mBindPhoneFragment = new BindPhoneFragment();
                }
                mCurrentFragment = mBindPhoneFragment;
                break;
            case LOGIN_TYPE_PASSWORD_LOGIN:
                if (mPasswordLoginFragment == null) {
                    mPasswordLoginFragment = new PasswordLoginFragment();
                }
                mCurrentFragment = mPasswordLoginFragment;
                break;
            case LOGIN_TYPE_INPUT_VERIFY_CODE:
                if (mVerifyInputFragment == null) {
                    mVerifyInputFragment = VerifyInputFragment.newInstance(mPhone, 1);
                } else {
                    ((VerifyInputFragment) mVerifyInputFragment).reset(mPhone);
                }
                mCurrentFragment = mVerifyInputFragment;
                break;
            case LOGIN_TYPE_INPUT_VERIFY_CODE_RESET:
                if (mVerifyInputFragment == null) {
                    mVerifyInputFragment = VerifyInputFragment.newInstance(mPhone, 2);
                } else {
                    ((VerifyInputFragment) mVerifyInputFragment).reset(mPhone);
                }
                mCurrentFragment = mVerifyInputFragment;
                break;
            case LOGIN_TYPE_INPUT_RESET_CODE:
                if (mResetPasswordFragment == null) {
                    mResetPasswordFragment = SetPasswordFragment.newInstance(mToken, mPhone, mSmsCode, 2);
                }else {
                    mResetPasswordFragment.setArgToken(mToken, mPhone, mSmsCode, 2);
                }
                mCurrentFragment = mResetPasswordFragment;
                break;
            case LOGIN_TYPE_SET_PASSWORD:
                if (mSetPasswordFragment == null) {
                    mSetPasswordFragment = SetPasswordFragment.newInstance(mToken, mPhone, mSmsCode, 1);
                }
                mCurrentFragment = mSetPasswordFragment;
                break;
            case LOGIN_TYPE_INVITE:
                if (mInviteFragment == null) {
                    mInviteFragment = new InviteCodeFragment();
                }
                mCurrentFragment = mInviteFragment;
                break;
        }
        try {
            if (mCurrentFragment != null) {
                if (!mCurrentFragment.isAdded()) {
                    transaction.add(R.id.fragment_container, mCurrentFragment);
                }
                transaction.show(mCurrentFragment).commitAllowingStateLoss();
            }
        } catch (Exception e) {
        }
    }
}
